#include "comm.hpp"
#include "Pipe.hpp"

int main()
{
    // 1. 获取key
    key_t key = GetShmKeyOrDie();
    std::cout << "key: " << To_Hex(key) << std::endl;
    // sleep(2);

    // 2. 创建共享内存
    int shmid = GetShm(key, defaultsize);
    std::cout << "shmid: " << shmid << std::endl;
    // sleep(2);

    // ShmDebug(shmid);
    // 4. 将共享内存和进程进行挂接(关联)
    char *addr = (char *)Shmattach(shmid);
    std::cout << "Attach shm success, addr: " << To_Hex((uint64_t)addr) << std::endl;

    memset(addr,0,defaultsize);
    Sync sync;
    sync.OpenWriteOrDie();

    // 可以进行通信了
    for (char c = 'A'; c <= 'Z'; c++) // pipe, fifo, ->read/write->系统调用， shm -> 没有使用系统调用！！
    {
        addr[c - 'A'] = c;
        sleep(1);
        sync.Wakeup();//传递可读信息
    }

    Shmdelete(addr);
    std::cout << "Detach shm success, addr: " << To_Hex((uint64_t)addr) << std::endl;
    sleep(5);

    return 0;
}